package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DeptFileArchived;
import com.ruoyi.system.domain.SysUserDevelop;
import com.ruoyi.system.service.IDeptFileArchivedService;
import com.ruoyi.system.service.ISysUserDevelopService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysUserStage4Mapper;
import com.ruoyi.system.domain.SysUserStage4;
import com.ruoyi.system.service.ISysUserStage4Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 预备党员阶段Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class SysUserStage4ServiceImpl implements ISysUserStage4Service
{
    @Autowired
    private SysUserStage4Mapper sysUserStage4Mapper;
    @Autowired
    private ISysUserDevelopService sysUserDevelopService;
    @Autowired
    private IDeptFileArchivedService deptFileArchivedService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询预备党员阶段
     *
     * @param id 预备党员阶段主键
     * @return 预备党员阶段
     */
    @Override
    public SysUserStage4 selectSysUserStage4ById(Long id)
    {
        return sysUserStage4Mapper.selectSysUserStage4ById(id);
    }

    /**
     * 查询预备党员阶段列表
     *
     * @param sysUserStage4 预备党员阶段
     * @return 预备党员阶段
     */
    @Override
    public List<SysUserStage4> selectSysUserStage4List(SysUserStage4 sysUserStage4)
    {
        return sysUserStage4Mapper.selectSysUserStage4List(sysUserStage4);
    }

    /**
     * 新增预备党员阶段
     *
     * @param sysUserStage4 预备党员阶段
     * @return 结果
     */
    @Override
    public int insertSysUserStage4(SysUserStage4 sysUserStage4)
    {
        sysUserStage4.setCreateTime(DateUtils.getNowDate());
        return sysUserStage4Mapper.insertSysUserStage4(sysUserStage4);
    }

    /**
     * 修改预备党员阶段
     *
     * @param sysUserStage4 预备党员阶段
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysUserStage4(SysUserStage4 sysUserStage4)
    {
        String createBy = sysUserStage4.getCreateBy();
        String userName = sysUserStage4.getDevelopUserName();
        sysUserStage4.setUpdateTime(DateUtils.getNowDate());
        SysUserStage4 sysUserStage = sysUserStage4Mapper.selectSysUserStage4ByDevelopId(sysUserStage4.getSysUserDevelopId());
        Date step12CheckTime = sysUserStage4.getStep12CheckTime();
        Date step13RecordTime = sysUserStage4.getStep13RecordTime();
        String step14File = sysUserStage4.getStep14File();
        Date step15PassTime = sysUserStage4.getStep15PassTime();
        Date step16TalkTime = sysUserStage4.getStep16TalkTime();
        Date step17CheckTime = sysUserStage4.getStep17CheckTime();
        String step18File1 = sysUserStage4.getStep18File1();
        SysUserDevelop sysUserDevelop = new SysUserDevelop();
        sysUserDevelop.setId(sysUserStage.getSysUserDevelopId());

        //父归档文件
        DeptFileArchived deptFileArchived = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(sysUserStage4.getSysUserDevelopId()));
        if(!ObjectUtils.isEmpty(step12CheckTime)){
            sysUserStage4.setStep12Status("1");
            sysUserStage4.setStep12FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("支部委员会审查阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段12归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("支部委员会审查阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage4.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段12文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("支委会审查发展对象会议纪要");
            deptFileArchivedVo2.setFileUrl(sysUserStage4.getStep12File1());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);

            DeptFileArchived deptFileArchivedVo3 = new DeptFileArchived();
            deptFileArchivedVo3.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo3.setCreateBy(createBy);
            deptFileArchivedVo3.setCreateName(userName);
            deptFileArchivedVo3.setFileType("1");
            deptFileArchivedVo3.setFileName("预审发展对象请示");
            deptFileArchivedVo3.setFileUrl(sysUserStage4.getStep12File2());
            deptFileArchivedVo3.setParentId(archivedId);
            deptFileArchivedVo3.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo3);
        }else if(!ObjectUtils.isEmpty(step13RecordTime)){
            sysUserStage4.setStep13Status("1");
            sysUserStage4.setStep13FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("上级党委预审阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段13归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("上级党委预审阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage4.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段13文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("基层党委预审意见");
            deptFileArchivedVo2.setFileUrl(sysUserStage4.getStep13File1());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);

            DeptFileArchived deptFileArchivedVo3 = new DeptFileArchived();
            deptFileArchivedVo3.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo3.setCreateBy(createBy);
            deptFileArchivedVo3.setCreateName(userName);
            deptFileArchivedVo3.setFileType("1");
            deptFileArchivedVo3.setFileName("接收中共预备党员公示");
            deptFileArchivedVo3.setFileUrl(sysUserStage4.getStep13File2());
            deptFileArchivedVo3.setParentId(archivedId);
            deptFileArchivedVo3.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo3);

            DeptFileArchived deptFileArchivedVo4 = new DeptFileArchived();
            deptFileArchivedVo4.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo4.setCreateBy(createBy);
            deptFileArchivedVo4.setCreateName(userName);
            deptFileArchivedVo4.setFileType("1");
            deptFileArchivedVo4.setFileName("预备党员公示结果确认表");
            deptFileArchivedVo4.setFileUrl(sysUserStage4.getStep13File1());
            deptFileArchivedVo4.setParentId(archivedId);
            deptFileArchivedVo4.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo4);
        }else if(StringUtils.isNotBlank(step14File)){
            sysUserStage4.setStep14Status("1");
            sysUserStage4.setStep14FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("填写入党志愿书阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段14归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("填写入党志愿书阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage4.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段14文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("入党志愿书");
            deptFileArchivedVo2.setFileUrl(sysUserStage4.getStep14File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }else if(!ObjectUtils.isEmpty(step15PassTime)){
            sysUserStage4.setStep15Status("1");
            sysUserStage4.setStep15FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("支部大会讨论阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段15归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("支部大会讨论阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage4.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段15文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("支部党员大会讨论接收预备党员会议议程");
            deptFileArchivedVo2.setFileUrl(sysUserStage4.getStep15File1());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);

            DeptFileArchived deptFileArchivedVo3 = new DeptFileArchived();
            deptFileArchivedVo3.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo3.setCreateBy(createBy);
            deptFileArchivedVo3.setCreateName(userName);
            deptFileArchivedVo3.setFileType("1");
            deptFileArchivedVo3.setFileName("接收预备党员会议纪要");
            deptFileArchivedVo3.setFileUrl(sysUserStage4.getStep15File2());
            deptFileArchivedVo3.setParentId(archivedId);
            deptFileArchivedVo3.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo3);

            DeptFileArchived deptFileArchivedVo4 = new DeptFileArchived();
            deptFileArchivedVo4.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo4.setCreateBy(createBy);
            deptFileArchivedVo4.setCreateName(userName);
            deptFileArchivedVo4.setFileType("1");
            deptFileArchivedVo4.setFileName("接收预备党员表绝票");
            deptFileArchivedVo4.setFileUrl(sysUserStage4.getStep15File3());
            deptFileArchivedVo4.setParentId(archivedId);
            deptFileArchivedVo4.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo4);

            DeptFileArchived deptFileArchivedVo5 = new DeptFileArchived();
            deptFileArchivedVo5.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo5.setCreateBy(createBy);
            deptFileArchivedVo5.setCreateName(userName);
            deptFileArchivedVo5.setFileType("1");
            deptFileArchivedVo5.setFileName("接收预备党员表决票结果");
            deptFileArchivedVo5.setFileUrl(sysUserStage4.getStep15File4());
            deptFileArchivedVo5.setParentId(archivedId);
            deptFileArchivedVo5.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo5);

            DeptFileArchived deptFileArchivedVo6 = new DeptFileArchived();
            deptFileArchivedVo6.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo6.setCreateBy(createBy);
            deptFileArchivedVo6.setCreateName(userName);
            deptFileArchivedVo6.setFileType("1");
            deptFileArchivedVo6.setFileName("接收中共预备党员决议");
            deptFileArchivedVo6.setFileUrl(sysUserStage4.getStep15File5());
            deptFileArchivedVo6.setParentId(archivedId);
            deptFileArchivedVo6.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo6);

            DeptFileArchived deptFileArchivedVo7 = new DeptFileArchived();
            deptFileArchivedVo7.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo7.setCreateBy(createBy);
            deptFileArchivedVo7.setCreateName(userName);
            deptFileArchivedVo7.setFileType("1");
            deptFileArchivedVo7.setFileName("接收中共预备党员请示");
            deptFileArchivedVo7.setFileUrl(sysUserStage4.getStep15File6());
            deptFileArchivedVo7.setParentId(archivedId);
            deptFileArchivedVo7.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo7);
        }else if(!ObjectUtils.isEmpty(step16TalkTime)){
            sysUserStage4.setStep16Status("1");
            sysUserStage4.setStep16FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("上级党委派人谈话阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段16归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("上级党委派人谈话阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage4.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段16文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("相关资料");
            deptFileArchivedVo2.setFileUrl(sysUserStage4.getStep16File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }else if(!ObjectUtils.isEmpty(step17CheckTime)){
            sysUserStage4.setStep17Status("1");
            sysUserStage4.setStep17FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("上级党委审查阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段17归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("上级党委审查阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage4.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段17文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("相关资料");
            deptFileArchivedVo2.setFileUrl(sysUserStage4.getStep17File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }else if(!ObjectUtils.isEmpty(step18File1)){
            sysUserStage4.setStep18Status("1");
            sysUserStage4.setStep18FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("再上一级单位组织部门备案阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段18归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("再上一级单位组织部门备案阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage4.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段18文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("预备党员备案报告");
            deptFileArchivedVo2.setFileUrl(sysUserStage4.getStep18File1());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);

            //创建阶段18文件
            DeptFileArchived deptFileArchivedVo3 = new DeptFileArchived();
            deptFileArchivedVo3.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo3.setCreateBy(createBy);
            deptFileArchivedVo3.setCreateName(userName);
            deptFileArchivedVo3.setFileType("1");
            deptFileArchivedVo3.setFileName("预备党员备案情况表");
            deptFileArchivedVo3.setFileUrl(sysUserStage4.getStep18File2());
            deptFileArchivedVo3.setParentId(archivedId);
            deptFileArchivedVo3.setAssociationId(String.valueOf(sysUserStage4.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        sysUserStage4.setId(sysUserStage.getId());
        return sysUserStage4Mapper.updateSysUserStage4(sysUserStage4);
    }

    /**
     * 删除预备党员阶段信息
     *
     * @param id 预备党员阶段主键
     * @return 结果
     */
    @Override
    public int deleteSysUserStage4ById(Long id)
    {
        return sysUserStage4Mapper.deleteSysUserStage4ById(id);
    }

    /**
     * 删除入党申请人阶段信息
     *
     * @param userDevelopId 入党申请人阶段主键s
     * @return 结果
     */
    @Override
    public int deleteSysUserStage4ByUserDevelopId(Long userDevelopId){
        return sysUserStage4Mapper.deleteSysUserStage4ByUserDevelopId(userDevelopId);
    }

    /**
     * 查询入党申请人阶段
     *
     * @param userDevelopId 入党申请人阶段主键
     * @return 入党申请人阶段
     */
    @Override
    public SysUserStage4 selectSysUserStage4ByDevelopId(Long userDevelopId){
        return sysUserStage4Mapper.selectSysUserStage4ByDevelopId(userDevelopId);
    }
}
